//
//  forgetPasswordViewController.m
//  FastRunDemo
//
//  Created by 苟亚明 on 16/9/21.
//  Copyright © 2016年 苟亚明. All rights reserved.
//

#import "forgetPasswordViewController.h"
NSString* const XCNextSendCheckCodeDate1 = @"nextSendCheckCodeDate1";
@interface forgetPasswordViewController ()
@property (weak, nonatomic) IBOutlet UIButton *btnSendCode;
/**
 *  验证码发送间隔定时器。
 */
@property (nonatomic, strong) NSTimer* sendCheckCodeIntervalTimer;

/**
 *  验证码发送间隔定时器的重复次数。
 */
@property (nonatomic) NSInteger timerRepeatCount;
@end

@implementation forgetPasswordViewController
- (IBAction)btnSendCodeClick:(id)sender {
    
    //禁止再次点击按钮。
    self.btnSendCode.enabled = NO;
    
    self.timerRepeatCount = 60;
    
    NSDate* nextSendCheckCodeDate = [NSDate dateWithTimeIntervalSinceNow:self.timerRepeatCount];
    
    //保存时间，下次进入该界面，如果不能发松，仍然显示倒计时。
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    
    [userDefaults setObject:nextSendCheckCodeDate forKey:XCNextSendCheckCodeDate1];
    
    [userDefaults synchronize];
    
    
    [self.sendCheckCodeIntervalTimer fire];
}
- (IBAction)btnRegistClick:(id)sender {
}

- (void)viewWillAppear:(BOOL)animated {
    
    //  self.navigationController.navigationBarHidden=NO;
    
    self.navigationController.navigationBar.hidden = NO;
    
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSDate* nextSendCheckCodeDate = [userDefaults objectForKey:XCNextSendCheckCodeDate1];
    
    NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:nextSendCheckCodeDate];
    
    if (timeInterval < 0) {
        self.btnSendCode.enabled = NO;
        
        self.timerRepeatCount = -timeInterval;
        
        [self.sendCheckCodeIntervalTimer fire];
    }else {
        self.btnSendCode.enabled = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"找回密码";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"btn_del" highImage:@"btn_del" target:self action:@selector(leftBtnClick)];

}
- (void)viewDidDisappear:(BOOL)animated {
    [self.sendCheckCodeIntervalTimer invalidate];
    
    self.sendCheckCodeIntervalTimer = nil;
}

- (void)leftBtnClick{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (void)changeSendCheckCodeButtonText:(NSTimer* )timer {
    self.timerRepeatCount--;
    [self.btnSendCode setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    if (self.timerRepeatCount > 0) {
        NSString* buttonTitle = [[NSString alloc] initWithFormat:@"%@S后重发", @(self.timerRepeatCount)];
        
        NSMutableAttributedString* buttonTitleAttributedTitle = [[NSMutableAttributedString alloc] initWithString:buttonTitle];
        NSInteger start = self.timerRepeatCount < 10 ? 2 : 3;
        
        [buttonTitleAttributedTitle addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, buttonTitle.length - 3)];
        
        [buttonTitleAttributedTitle addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(start, 3)];
        [self.btnSendCode setAttributedTitle:buttonTitleAttributedTitle forState:UIControlStateDisabled];
    }else {
        self.btnSendCode.enabled = YES;
        
        [self.btnSendCode setAttributedTitle:nil forState:UIControlStateDisabled];
        
        [self.sendCheckCodeIntervalTimer invalidate];
        
        self.sendCheckCodeIntervalTimer = nil;
    }
}
- (NSTimer* )sendCheckCodeIntervalTimer {
    if (_sendCheckCodeIntervalTimer == nil) {
        _sendCheckCodeIntervalTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(changeSendCheckCodeButtonText:) userInfo:nil repeats:YES];
        
        [[NSRunLoop currentRunLoop] addTimer:_sendCheckCodeIntervalTimer forMode:NSDefaultRunLoopMode];
    }
    
    return _sendCheckCodeIntervalTimer;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
@end
